<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use const Zipkin\Kind\PRODUCER;

/**
 * @property int $sku_id
 * @property int $shop_id 商户id
 * @property string $sku_name sku名称
 * @property int $product_id 商品id
 * @property string $weight kg重量
 * @property string $volume 立方米
 * @property int $is_step_price 是否阶梯价
 * @property int $inventory 库存
 * @property int $min_quantity 起订量
 * @property string $single_price 单价
 * @property int $fileinfo_id 文件系统id
 * @property int $sales_volume 销量
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property string $deleted_at 删除时间
 * @property int $create_by 创建人
 * @property int $update_by 修改人
 * @property int $delete_by 删除人
 */
class Sku extends CloudBase
{
    protected $primaryKey = 'sku_id';
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_sku';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['sku_id' => 'string', 'shop_id' => 'string', 'product_id' => 'integer', 'is_step_price' => 'integer', 'inventory' => 'integer',
        'min_quantity' => 'integer', 'fileinfo_id' => 'string', 'sales_volume' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime',
        'create_by' => 'string', 'update_by' => 'string', 'delete_by' => 'string',
        'audit_id' => 'string'
    ];

    public function spec_value()
    {
        $fields = ['fly_product_spec_value.*', 'key.spec_name'];
        return $this->hasMany(ProductSpecValue::class, 'sku_id', 'sku_id')
            ->leftJoin('fly_product_spec_key as key', 'key.id', '=', 'fly_product_spec_value.spec_key')
            ->select($fields);
    }

    /**
     * 关联sku阶梯价
     * @return HasMany
     */
    public function step_price()
    {
        return $this->hasMany(SkuPrice::class, 'sku_id', 'sku_id')
            ->orderBy('num')
            ->select();
    }

    /**
     * 关联sku对应商品
     * @return HasOne
     */
    public function with_product()
    {
        return $this->hasOne(Product::class, 'id', 'product_id')
            ->with(['with_shop', 'main_pic', 'landmark_info', 'industry_info', 'cate_info'])
            ->whereNull('fly_product.deleted_at');
    }
}